package com.jemmyee.cms.web.controller.front;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.jemmyee.cms.entity.Article;
import com.jemmyee.cms.entity.ProductBrand;
import com.jemmyee.cms.web.CmsConstants;
import com.jemmyee.cms.web.controller.kernel.BaseCmsController;
import com.jemmyee.core.dao.Page;
import com.jemmyee.core.web.Constants;
import com.jemmyee.core.web.utils.MathUtils;


/**
 * @Description:门户系统前台
 * @author:jemmyee@gmail.com
 * @date:2011-8-24
 * @version:v1.0
 */
@Controller("brandFrontIndexController")
@RequestMapping(Constants.MV_URI_APP_CMS+CmsConstants.MV_URI_THEME_BRAND) 
public class IndexBrandController extends BaseCmsController{
	
	private static Log log= LogFactory.getLog(IndexBrandController.class);
	/*分页查出保存的条件*/
	private static final String ENTITY_SESSION_BEAN="admin_session_bean_"+MathUtils.getRandomDouble();
	private static final String ROOT_VIEW=Constants.MV_URI_MAIN_FRONT+CmsConstants.MV_VIEW_CMS+CmsConstants.MV_URI_THEME_BRAND+Constants.SYM_PATH_SEPARATOR;

	@RequestMapping("/index")
	public ModelAndView index(HttpServletRequest request){
		ModelAndView mv=new ModelAndView();
		mv.addObject("brands", productBrandService.findByProperty("showIndex",(short)1));
		mv.setViewName(ROOT_VIEW+"/index"); 
		return mv;
	}
	@RequestMapping("/header")
	public ModelAndView member(HttpServletRequest request){
		ModelAndView mv=new ModelAndView();
		mv.setViewName(ROOT_VIEW+"/header"); 
		return mv;
	}
	
	@RequestMapping("/service")
	public ModelAndView service(HttpServletRequest request){
		ModelAndView mv=new ModelAndView();
		mv.setViewName(ROOT_VIEW+"/service"); 
		return mv;
	}
	@RequestMapping("/cates")
	public ModelAndView cates(HttpServletRequest request){
		ModelAndView mv=new ModelAndView();
		mv.addObject("cates", productCategoryService.findAll());
		mv.setViewName(ROOT_VIEW+"/cates"); 
		return mv;
	}
	@RequestMapping("/news/detail/{id}")
	public ModelAndView newsDetail(@PathVariable String id,HttpServletRequest request){
		ModelAndView mv=new ModelAndView();
		Article item=articleService.findById(id);
		mv.addObject("item",item);
		mv.addObject("category",articleCategoryService.findById(item.getCategoryId()));
		mv.setViewName(ROOT_VIEW+"/news_detail"); 
		return mv;
	}
	@RequestMapping("/news/single/{id}")
	public ModelAndView newsSingle(@PathVariable String id,HttpServletRequest request){
		ModelAndView mv=new ModelAndView();
		Article item=articleService.findById(id);
		mv.addObject("item",item);
		mv.addObject("items",articleService.findByProperty("categoryId", 1));
		mv.setViewName(ROOT_VIEW+"/single"); 
		return mv;
	}
	
	@RequestMapping("/brand/detail/{id}")
	public ModelAndView brandDetail(@PathVariable String id,HttpServletRequest request){
		ModelAndView mv=new ModelAndView();
		mv.addObject("item",productBrandService.findById(id));
		mv.setViewName(ROOT_VIEW+"/brand_detail"); 
		return mv;
	}
	
	@RequestMapping("/footer")
	public ModelAndView left(HttpServletRequest request){
		ModelAndView mv=new ModelAndView();
		mv.addObject("links",linkService.findAll());
		mv.setViewName(ROOT_VIEW+"/footer"); 
		return mv;
	}
	@RequestMapping("/right")
	public ModelAndView footer(HttpServletRequest request){
		ModelAndView mv=new ModelAndView();
		mv.addObject("tongzhi",articleService.findByProperty("categoryId",6));
		mv.addObject("xinwen",articleService.findByProperty("categoryId",2));
		mv.addObject("anli",articleService.findByProperty("categoryId",5));
		mv.setViewName(ROOT_VIEW+"/right"); 
		return mv;
	}
	@RequestMapping("/news/{from}/{parentId}/{pageSize}/{startIndex}")
	public ModelAndView news(@PathVariable String from,
			@PathVariable Integer parentId, @PathVariable Integer pageSize,
			@PathVariable Integer startIndex, @ModelAttribute Article bean,
			HttpServletRequest request) {
		DetachedCriteria dc = DetachedCriteria.forClass(Article.class);
		ModelAndView mv = new ModelAndView();
		Article temp = new Article();
		if (from.equals(Constants.PAGE_ENTRY_TYPE_FORM)) {
			temp = bean;
			request.getSession().setAttribute(ENTITY_SESSION_BEAN, bean);
		}
		if (from.equals(Constants.PAGE_ENTRY_TYPE_PAGE)) {
			if (request.getSession().getAttribute(ENTITY_SESSION_BEAN) != null) {
				temp = (Article) request.getSession().getAttribute(
						ENTITY_SESSION_BEAN);
			}
		}
		if (from.equals(Constants.PAGE_ENTRY_TYPE_MENU)) {

		}
		if (parentId != 0) {
			dc.add(Restrictions.eq("categoryId", parentId));
		}
		if (pageSize == 0) {
			pageSize = Constants.DEFAULT_PAGESIZE;
		}
		if (bean.getBkw() != null && !bean.getBkw().equals("")) {
			dc.add(Restrictions.like("title", "%" + bean.getBkw() + "%"));
		}
		if (bean.getStartTime() != null && bean.getEndTime() != null) {
			dc.add(Restrictions.ge("addTime", bean.getStartTime()));
			dc.add(Restrictions.le("addTime", bean.getEndTime()));
		}
		if (bean.getStartTime() != null && bean.getEndTime() == null) {
			dc.add(Restrictions.ge("addTime", bean.getStartTime()));
		}
		if (bean.getStartTime() == null && bean.getEndTime() != null) {
			dc.add(Restrictions.le("addTime", bean.getEndTime()));
		}
		dc.addOrder(Order.desc("addTime"));
		Page page = articleService.findPageByCriteria(dc, pageSize, startIndex);
		if (pageSize != 0) {
			page.setPageSize(pageSize);
		}
		temp.setHightLight(Constants.HIGHTLIGHT_PREFIX + bean.getBkw()
				+ Constants.HIGHTLIGHT_SUFFIX);
		mv.addObject(Constants.MV_OBJECT_PAGE, page);
		mv.addObject(Constants.MV_OBJECT_ENTITY, "");
		mv.addObject("parentId",parentId);
		mv.addObject("pageSize",pageSize);
		mv.addObject("startIndex",startIndex);
		mv.addObject("category",articleCategoryService.findById(parentId));
		mv.setViewName(ROOT_VIEW + "news");
		return mv;
	}
	@RequestMapping("/brands/{from}/{parentId}/{pageSize}/{startIndex}")
	public ModelAndView brands(@PathVariable String from,
			@PathVariable Integer parentId, @PathVariable Integer pageSize,
			@PathVariable Integer startIndex, @ModelAttribute Article bean,
			HttpServletRequest request) {
		DetachedCriteria dc = DetachedCriteria.forClass(ProductBrand.class);
		ModelAndView mv = new ModelAndView();
		Article temp = new Article();
		if (from.equals(Constants.PAGE_ENTRY_TYPE_FORM)) {
			temp = bean;
			request.getSession().setAttribute(ENTITY_SESSION_BEAN, bean);
		}
		if (from.equals(Constants.PAGE_ENTRY_TYPE_PAGE)) {
			if (request.getSession().getAttribute(ENTITY_SESSION_BEAN) != null) {
				temp = (Article) request.getSession().getAttribute(
						ENTITY_SESSION_BEAN);
			}
		}
		if (from.equals(Constants.PAGE_ENTRY_TYPE_MENU)) {
			
		}
		if (parentId != 0) {
			dc.add(Restrictions.eq("categoryId", parentId));
		}
		if (pageSize == 0) {
			pageSize = Constants.DEFAULT_PAGESIZE;
		}
		dc.addOrder(Order.desc("addTime"));
		Page page = articleService.findPageByCriteria(dc, pageSize, startIndex);
		if (pageSize != 0) {
			page.setPageSize(pageSize);
		}
		mv.addObject(Constants.MV_OBJECT_PAGE, page);
		mv.addObject(Constants.MV_OBJECT_ENTITY, "");
		mv.addObject("parentId",parentId);
		mv.addObject("pageSize",pageSize);
		mv.addObject("startIndex",startIndex);
		mv.addObject("category",productCategoryService.findById(parentId));
		mv.setViewName(ROOT_VIEW + "brands");
		return mv;
	}
}
